import IsWeixinOrAlipay from "./IsWeixinOrAlipay.js"
import jwx from '@/common/jwx'

function sysstr(result) {
	function urlfx(result, name) {
		let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		let r = result.split("?")[1];
		if (r) {
			r = result.split("?")[1].match(reg);
			if (r) return decodeURI(r[2]);
			return null;
		}
	}
	let gtel = urlfx(result, "gtel");
	if (gtel) {
		uni.reLaunch({
			url: `/pages/ChargeCarSys/ChargeCarSys?gtel=${gtel}`
		});
	} else {
		uni.showToast({
			title: '扫描失败',
			duration: 2000,
			icon: "none"
		});
	}
}
export default function(callback) {
	let promise;
	if (typeof callback !== "function") {
		promise = new Promise((resolve, reject) => {
			callback = function(resolvedata, rejectdata) {
				if (resolvedata) {
					resolve(resolvedata);
				} else {
					reject(rejectdata);
				}
			};
		});
	}
	//#ifndef H5
	uni.scanCode({
		success: function(res) {
			console.log(res);
			sysstr(res.result);
		},
		complete: function(res) {
			console.log(2222);
			console.log(res);
		}
	});
	//#endif
	//#ifdef H5
	if (IsWeixinOrAlipay() == "weixin") {
		jwx.sysscanQRCode();
	} else if (IsWeixinOrAlipay() == "zfb") {
		ap.scan(function(res) {
			if (res.code != "" && res.code) {
				let result = res.code;
				sysstr(result);
			}
		});
	} else {
		console.log('有吗')
		uni.showToast({
			title: uni.$z.$t('请在微信或支付宝中打开'),
			duration: 2000,
			icon: "none"
		});

	}
	//#endif
	return promise;
}